﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LetsDoIt.Controls
{
    public partial class ProgressBarEx : UserControl
    {

        #region Transparent Color
        Color _transparentColor;
        /// <summary>
        /// Sets or gets transparent color for an Image. The default color is 255, 0, 255.
        /// </summary>
        public Color TransparentColor
        {
            get
            {
                return _transparentColor;
            }
            set
            {
                _transparentColor = value;
                this.Invalidate();
            }
        }
        #endregion

        #region Progress Color
        Color _progressColor;
        /// <summary>
        /// Sets or gets transparent color for an Image. The default color is 255, 0, 255.
        /// </summary>
        public Color ProgressColor
        {
            get
            {
                return _progressColor;
            }
            set
            {
                _progressColor = value;
                this.Invalidate();
            }
        }
        #endregion


        Color _borderColor;
        public Color BorderColor
        {
            get
            {
                return _borderColor;
            }
            set
            {
                _borderColor = value;
                this.Invalidate();
            }
        }

        int _borderWidth;
        public int BorderWidth
        {
            get
            {
                return _borderWidth;
            }
            set
            {
                _borderWidth = Math.Max(value, 0);
                this.Invalidate();
            }
        }

        private int maxValue = 100;
        public int MaxValue
        {
            get { return maxValue; }
            set
            {
                maxValue = Math.Max( value, minValue);
                this.Invalidate();
            }
        }

        private int minValue;
        public int MinValue
        {
            get { return minValue; }
            set
            {
                minValue = Math.Max(Math.Min(value, maxValue), 0);
                this.Invalidate();
            }
        }

        private int value;
        public int Value
        {
            get { return value; }
            set
            {
                this.value = Math.Min(maxValue, Math.Max(value, minValue));
                this.Invalidate();
            }
        }

        private string text;
        public new string Text
        {
            get { return text; }
            set
            {
                text = value;
                this.Invalidate();
            }
        }

        public ProgressBarEx()
        {
            InitializeComponent();
            _transparentColor = Color.FromArgb(255, 0, 255);
            _progressColor = Color.CornflowerBlue;
            _borderColor = Color.DarkGray;
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.Clear(this.BackColor);

            SolidBrush progressBrush = new SolidBrush(_progressColor);
            Rectangle recProgress = new Rectangle(0,0, (int)(this.Width * ((double)value / (double)(maxValue - minValue))), this.Height);
            e.Graphics.FillRectangle(progressBrush, recProgress);
            progressBrush.Dispose();
			
            if (_borderWidth > 0)
            {
                Pen penBorder = new Pen(_borderColor, _borderWidth);
                Rectangle recBorder = new Rectangle(_borderWidth / 2, _borderWidth/2, this.Width - _borderWidth, this.Height - _borderWidth);
                e.Graphics.DrawRectangle(penBorder, recBorder);
                penBorder.Dispose();
            }

            if (!string.IsNullOrEmpty(text))
            {
                //write text
                SolidBrush textBrush = new SolidBrush(this.ForeColor);
                StringFormat sf = new StringFormat();
                SizeF fontsize = e.Graphics.MeasureString(text, this.Font);
                e.Graphics.DrawString(text, this.Font, textBrush,
                                      (this.Width-fontsize.Width)/2,
                                      (this.Height- fontsize.Height)/2
                    );

                textBrush.Dispose();
            }
        }
		
        public new void Dispose()
        {
            //
        }
    }
}